package edu.pku.sei.act.taskStruct;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.dom4j.Element;

public class InitTransition extends Transition {
	
	private Map<String, String> postRolesAndStates;
	
	private void setDefaultVal()
	{
		postRolesAndStates = new TreeMap<String, String>();
	}
	
	public InitTransition(Element element)
	{
		super();
		setDefaultVal();
		setIdAndName(element);
		
		Element ele;
		
		ele = element.element(TagNames.TYPE);
		if(ele != null) this.type = ele.getTextTrim();
		
		ele = element.element(TagNames.ABILITY_OUTPUT_ID);
		if(ele != null) this.abilityOutputId = ele.getTextTrim();
		
		ele = element.element(TagNames.PRE_TASK_MESSAGE_ID);
		if(ele != null) this.preTaskMessageId = ele.getTextTrim();
		
		ele = element.element(TagNames.PRE_STATES);	
		
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.STATE_ID);
			for(Element e: list)
			{
				String prestate = e.getTextTrim();
				if(!"".equals(prestate))
					preStateIds.add(prestate);
			}
		}
		
		ele = element.element(TagNames.POST_STATES);
		if(ele != null)
		{
			List<Element> list = ele.elements(TagNames.POST_STATE);
			for(Element e: list)
			{				
				Element e_id = e.element(TagNames.ROLE_ID);
				if(e_id != null && !"".equals(e_id.getTextTrim()))
				{					
					Element e_cond = e.element(TagNames.CONDITION);
					String cond = "";
					if(e_cond != null) cond = e_cond.getTextTrim(); 
					postStateConditions.put(e_id.getTextTrim(), cond);
					
					Element e_state = e.element(TagNames.STATE_ID);
					String state_id = "";
					if(e_state != null) state_id = e_state.getTextTrim();
					postRolesAndStates.put(e_id.getTextTrim(), state_id);
					
					List<Element> eList = e.elements(TagNames.TASK_MESSAGE_ID);
					Set<String> msg_ids = new TreeSet<String>();
					for(Element e_msg: eList)
					{
						String msg_id = e_msg.getTextTrim();
						if(!"".equals(msg_id)) msg_ids.add(msg_id);							
					}
					postTaskMessageIds.put(e_id.getTextTrim(), msg_ids);
				}
			}
		}			
	}
		
	public InitTransition() {		
		super();
		setDefaultVal();
	}

	public void addAttrisAndEles(Element element)
	{
		addAttributes(element);
		
		Element ele;
		ele = element.addElement(TagNames.TYPE);
		ele.addText(type);
		ele = element.addElement(TagNames.ABILITY_OUTPUT_ID);
		ele.addText(abilityOutputId);
		ele = element.addElement(TagNames.PRE_TASK_MESSAGE_ID);
		ele.addText(preTaskMessageId);
				
		ele = element.addElement(TagNames.PRE_STATES);
		for(String stateId: preStateIds)
		{
			Element e;
			e = ele.addElement(TagNames.STATE_ID);
			e.addText(stateId);
		}
		ele = element.addElement(TagNames.POST_STATES);
		Set<Map.Entry<String, String>> pRoles = postRolesAndStates.entrySet();
		for(Map.Entry<String, String> en: pRoles)
		{
			Element e;
			e = ele.addElement(TagNames.ROLE_ID);
			e.addText(en.getKey());
			
			e = ele.addElement(TagNames.STATE_ID);
			e.addText(en.getValue());
												
			e = ele.addElement(TagNames.CONDITION);
			e.addText(postStateConditions.get(en.getValue()));			
			
			Set<String> msg_ids = postTaskMessageIds.get(en.getKey());
			for(String msg_id: msg_ids)
			{				
				e = ele.addElement(TagNames.TASK_MESSAGE_ID);
				e.addText(msg_id);
			}
		}				
	}
	
	public Set<String> getPostRoleIds()
	{
		return postRolesAndStates.keySet();
	}
	
	public String getPostStateIdById(String roleId)
	{
		return postRolesAndStates.get(roleId);
	}	
}
